<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;//导入数据库类(Db类)
class Image extends Controller
{
    public function image()
    {
    	 // 使用Db类
        $list = Db::table('boke_album')->select();
		$this->assign('list',$list);
	      // return $this->fetch();
		return view('image/image');
       
    }

    public function addimage($pid)
    {
    	$this->assign('pid',$pid);
    	return $this->fetch('image/addimage');
    }

    public function album()
    {
    	return $this->fetch('image/album');
    }

    public function add()
    {
    	//判断表单是否为空
			foreach($_POST as $v){
		 			if($v == ''){
		 				$this->error('表单未填写完整');
		 				exit;
		 			}
			}
    		//拼接字段
			$field_key = '';
			$field_value = '';
			foreach($_POST as $k => $v){
					$field_key .= '`'.$k.'`,';
					$field_value .= '"'.$v.'",';
			}
			$field_key .= '`ctime`';
			$field_value .= time();
			// dump( $field_value);
			// exit;

			if(Db::execute('insert into boke_album('.$field_key.') values('.$field_value.')'))
				{
					$this->success('新增成功', 'image/image');
				}else{
					$this->success('新增相册失败');
					exit;
				}
    }
    public function images($id)
    {
    	 // 使用Db类
   $pid= $id;
        $list = Db::table('boke_image')->where('pid',$pid)->select();
		$this->assign('list',$list);
		$this->assign('pid',$pid);
	      // return $this->fetch();
		return view('image/images');
    }

	public function upload($pid){
		// 获取表单上传文件
		$files = request()->file('image');
		foreach($files as $file){
		// 移动到框架应用根目录/public/uploads/ 目录下
		$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
		if($info){
		// 成功上传后 获取上传信息
		$name = $info->getSaveName();
		// echo $name;
		$data = ['pid' => $pid, 'name' => $name];

		Db::table('boke_image')->insert($data);

		}else{
		// 上传失败获取错误信息
		echo $file->getError();
		}
		}
		}

}

